home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / net / bind-contrib.tar.gz / bind-contrib.tar / contrib / tic / readinfo < prev    next >
Text File  |  1996-10-25  |  4KB  |  178 lines

  1. #!/bin/sh
  2. # @(#) readinfo 1.3 92/08/31 @(#)
  3. # reads a host database file
  4. #
  5. # input from stdin
  6. # output to stdout
  7. #
  8. # arguments are names of fields in the file
  9. #
  10. # fields in a file are defined by a comment line of the form
  11. # #FIELDS <field_description> ...
  12. #
  13. # where <field_description> is the name of the field followed by optional
  14. # keyword parameters of the form parameter=<value>
  15. # three paramaters are supported in this script
  16. #    prefix - prefix added to value of the field
  17. #    suffix - suffix added to value of field
  18. #    no - character to prevent adding a prefix or suffix to a field
  19. #
  20. # fields in the file are separated with white space.  Embedded blanks in fields
  21. # are either escaped with a '\' or the entire field is quoted with double quotes
  22. # The prefix or suffix can be overridden by prepending or following the value
  23. # with the ``no='' character
  24.  
  25. # Copyright (c) 1992 by Texas Internet Consulting
  26. # This code may be freely copied and used so long as this
  27. # copyright notice is attached.  This code may not be sold
  28. # without the express written permission of Texas Internet Consulting.
  29. # Texas Internet Consulting makes no warranty as to the correctness
  30. # nor the applicability of this code for any purpose.
  31.  
  32. # get the fields from the command line - will be funnelled to awk
  33. fields="$*"
  34. # change whitespace to tabs and handle quoted fields and escaped blanks
  35. # also remove comment lines, except the #FIELDS line
  36. awk '
  37. BEGIN {
  38.     # get around double quote bug
  39.     dq = sprintf("%c", 34)
  40. }
  41. /^#FIELDS/ || ( ! /^#/ && ! /^$/ && ! /^[     ][     ]*/ ) {
  42. # line has double quotes or backslashes
  43.     if ($0 ~ /"/ || $0 ~ /\\/) {
  44.         # print everything up to field with double quote or backslash
  45.         l = length($0)
  46.         n = 0
  47.         for (i=1; i<=NF; i++) {
  48.             if ($i !~ /"/ && $i !~ /\\/) {
  49.                 printf("%s\t", $i)
  50.             }
  51.             n += length($i)
  52.             n++
  53.         }
  54.         remainder = substr($0, n+1, l-n)
  55.         l -= n
  56.         quoted = 0
  57.         escaped = 0
  58.         for (i=1; i<=l; i++) {
  59.             sub = substr(remainder, i, 1)
  60.             if (sub == dq) {
  61.                 if (quoted) {
  62.                     quoted = 0
  63.                 }
  64.                 else {
  65.                     quoted = 1
  66.                 }
  67.             }
  68.             else if (sub == "\\") {
  69.                 escaped = 1
  70.             }
  71.             else if (quoted) {
  72.                 printf("%s", sub)
  73.             }
  74.             else if (escaped) {
  75.                 printf("%s", sub)
  76.                 escaped = 0
  77.             }
  78.             else if (sub == " ") {
  79.                 printf("\t")
  80.             }
  81.             else {
  82.                 printf("%s", sub)
  83.             }
  84.         }
  85.     }
  86.     # line is just a bunch of normal fields
  87.     else {
  88.         for (i=1; i<=NF; i++) {
  89.             printf("%s", $i)
  90.             if (i < NF) {
  91.                 printf("\t")
  92.             }
  93.         }
  94.     }    
  95.     printf("\n")
  96. }' |
  97. # read file and output fields wanted
  98. awk -F'    ' '
  99. # get the fields from the command line
  100. BEGIN {
  101.     EXTRACT="'"$fields"'"
  102.     nextract = split(EXTRACT, extract, " ")
  103. }
  104. # extract field definition info from file
  105. /^#FIELDS/ {
  106.     nfields = 0
  107.     for (i=2; i<=NF; i++) {
  108.         # field is a paramter
  109.         if ($i ~ /\=/) {
  110.             n = split($i, part, "=")
  111.             keyword = part[1]
  112.             value = part[2]
  113.             # prefix
  114.             if (keyword == "prefix") {
  115.                 prefix[nfields] = value
  116.             }
  117.             # suffix
  118.             else if (keyword == "suffix") {
  119.                 suffix[nfields] = value
  120.             }
  121.             else if (keyword == "no") {
  122.                 no[nfields] = value
  123.             }
  124.         }
  125.         #field name
  126.         else {
  127.             nfields++
  128.             fields[nfields] = $i
  129.             prefix[nfields] = ""
  130.             suffix[nfields] = ""
  131.             no[nfields] = ""
  132.         }
  133.     }
  134. }
  135. # process a record
  136. ! /^#/ && ! /^$/ && ! /^[     ][     ]*/ {
  137.     # scan the list of fields to extract and compare them with the
  138.     # list in the file and print as they are encountered
  139.     for (i=1; i<=nextract; i++) {
  140.         for (j=1; j<=nfields; j++) {
  141.             if (fields[j] == extract[i]) {
  142.                 break
  143.             }
  144.         }
  145.         if (j > nfields) {
  146.             printf("***ERROR*** field not found - %s\n", extract[i])
  147.             continue
  148.         }
  149.         if (i > 1) printf("    ")
  150.         # no overide character for this field
  151.         if (no[j] == "") {
  152.             printf("%s%s%s", prefix[j], $j, suffix[j])
  153.             continue
  154.         }
  155.         # overide character exist - split up the field
  156.         n = length($j)
  157.         first = substr($j, 1, 1)
  158.         last = substr($j, n, 1)
  159.         # no overide character in field value
  160.         if (first != no[j] && last != no[j]) {
  161.             printf("%s%s%s", prefix[j], $j, suffix[j])
  162.             continue
  163.         }
  164.         middle = ""
  165.         if (n >= 3) {
  166.             middle = substr($j, 2, n-2)
  167.         }
  168.         if (first != no[j]) {
  169.             printf("%s%s", prefix[j], first)
  170.         }
  171.         printf("%s", middle)
  172.         if (last != no[j]) {
  173.             printf("%s%s", last, suffix[j])
  174.         }
  175.     }
  176.     printf("\n")
  177. }'
  178.